package com.eurasia.assignment.domain;

import java.time.LocalDateTime;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 项目领域对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Project {
    private Long id;
    private String name;
    private String type;
    private String course;
    private ProjectStatus status;
    private Integer points;
    private String creator;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private LocalDateTime startTime;
    private LocalDateTime finishTime;
    private String comment;
    private Long fileId; // 关联的文件ID

    /**
     * 创建新项目
     */
    public static Project create(String name, String type, String courseId, Integer points, String creator) {
        Project project = new Project();
        project.setName(name);
        project.setType(type);
        project.setCourse(courseId);
        project.setStatus(ProjectStatus.CREATED);
        project.setPoints(points);
        project.setCreator(creator);
        return project;
    }
    
    /**
     * 创建带有所有字段的新项目
     */
    public static Project createFullProject(String name, String type, String courseId, Integer points, 
                                           String creator, String startTimeStr, String finishTimeStr, 
                                           String comment, Long fileId) {
        Project project = new Project();
        project.setName(name);
        project.setType(type);
        project.setCourse(courseId);
        project.setStatus(ProjectStatus.CREATED);
        project.setPoints(points);
        project.setCreator(creator);
        
        // 处理开始和结束时间
        if (startTimeStr != null && !startTimeStr.isEmpty()) {
            try {
                project.setStartTime(LocalDateTime.parse(startTimeStr));
            } catch (Exception e) {
                // 如果解析错误，使用null
            }
        }
        
        if (finishTimeStr != null && !finishTimeStr.isEmpty()) {
            try {
                project.setFinishTime(LocalDateTime.parse(finishTimeStr));
            } catch (Exception e) {
                // 如果解析错误，使用null
            }
        }
        
        project.setComment(comment);
        project.setFileId(fileId);
        project.setCreateTime(LocalDateTime.now());
        project.setUpdateTime(LocalDateTime.now());
        
        return project;
    }

    /**
     * 创建项目分配
     * @param teamLead 团队负责人ID
     * @param teamMembers 团队成员ID列表
     * @return 创建的项目分配
     */
    public ProjectAssignment createAssignment(Long teamLead, List<Long> teamMembers) {
        // 创建项目分配
        ProjectAssignment assignment = ProjectAssignment.create(this.id);
        
        // 添加团队负责人
        assignment.addTeamLead(teamLead);
        
        // 添加团队成员
        if (teamMembers != null) {
            for (Long memberId : teamMembers) {
                assignment.addTeamMember(memberId, TeamRole.TEAM_MEMBER);
            }
        }
        
        // 更新项目状态
        this.status = ProjectStatus.IN_PROGRESS;
        
        return assignment;
    }
}